<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="会员名称">
        <el-input v-model="queryParams.memberUsername" placeholder="请输入会员名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="couponName" header-align="center" align="center" label="优惠券名称"></el-table-column>
      <el-table-column prop="memberUsername" header-align="center" align="center" label="会员姓名"></el-table-column>
      <el-table-column prop="getType" header-align="center" align="center" label="领取方式">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.receiveType === 1">自动发放</el-tag>
          <el-tag type="success" v-else>手动领取</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="领取时间"></el-table-column>
      <el-table-column prop="used" header-align="center" align="center" label="使用状态">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.used === 0">未使用</el-tag>
          <el-tag type="success" v-if="scope.row.used === 1">已使用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="useTime" header-align="center" align="center" label="使用时间">
        <template slot-scope="scope">
          <span>{{ scope.row.useTime ? scope.row.useTime: '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderSn" header-align="center" align="center" label="订单编号">
        <template slot-scope="scope">
          <span>{{ scope.row.orderSn ? scope.row.orderSn: '-' }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />
  </div>
</template>

<script>
import { couponHistoryList, deleteCouponHistory } from '@/api/coupon/couponHistory'

export default {
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        memberUsername: ""
      },
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    // 获取数据列表
    loadData() {
      this.loading = true;
      couponHistoryList(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records
          this.total = res.data.total
        }
        this.loading = false;
      });
    },
    selectionChangeHandle(val) {
      this.selections = val;
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.selections.map(item => {return item.id;});
      this.$confirm(
        '确认删除选中数据吗?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteCouponHistory(ids).then(res => {
          if (res.success) {
            this.loadData();
            this.$message.success('删除成功')
          }
        });
      });
    }
  }
};
</script>
